import net.aetherial.context.group.*;

class net.aetherial.context.group.Group
{
	public var name:String;
	public var members:Object;
	
	public var listeners:Array = new Array ();

	public function update ()
	{
		for (var i = 0; i < listeners.length; i++)
			listeners[i].redraw = true;
	}
	
	public function getMembers ():Array
	{
		var memberArray = new Array ();
		
		for (var name in members)
		{
			memberArray.push (members[name]);
		}
			
		return memberArray;
	}
	
	public function setXmlPath (xmlPath:String)
	{
		var me = this;
		
		var xml:XML = new XML ();
		
		xml.onLoad = function (success:Boolean)
		{
			var group:XML = this.firstChild;
			
			me.name = group.attributes.name;

			me.members = new Object ();			

			var children:Array = group.childNodes;
			
			for (var i:Number = 0; i < children.length; i++)
			{
				var node:XMLNode = children[i];
				
				if (node.nodeName == "user")
				{
					var nodeName:String = node.attributes.name;
					var dateString:String = node.attributes.date;
					var field:String = node.attributes.field;
					var value:String = node.attributes.value;
					var iconUrl:String = node.attributes.icon;

					var member:Member = undefined;
					
					for (var name:String in me.members)
					{
						if (name == nodeName)
							member = me.members[nodeName];
					}
					
					if (member == undefined)
					{
						member = new Member ();
						member.name = nodeName;
						member.iconUrl = iconUrl;
						
						me.members[nodeName] = member;

					}

					member.addUpdate (dateString, field, value);
				}
			}
			
			me.update ();
		}
		
		xml.load (xmlPath);
	}
	
	public function toString ()
	{
		var string:String = "";
		
		string += "-- Group Object --" + newline;
		string += "Name: " + this.name + newline;
		string += "Members" + newline;
		
		for (var name in members)
		 	string += members[name] + newline;
		
		return string;
	}
}

